package edu.biwu.mapper;

import edu.biwu.po.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 王俊彪
 * @version 1.0
 */
public interface ProductMapper {
    /**
     * keyProperty:主键对应属性
     * @param product
     */
    @SelectKey(keyProperty = "id",keyColumn = "id",resultType = int.class,statement = "SELECT LAST_INSERT_ID()",before = false)
    @Insert("INSERT INTO mybatis03.product(name, price, detail, pic, createtime) VALUES " +
            "(#{name},#{price},#{detail},#{pic},#{createtime})")
    void saveProduct(Product product);

    @Update("UPDATE mybatis03.product SET name = #{name}," +
            "price = #{price}," +
            "detail = #{detail}," +
            "pic = #{pic}," +
            "createtime = #{createtime}" +
            " WHERE id=#{id}")
    void updateProduct(Product product);

    @Delete("DELETE FROM mybatis03.product WHERE id = #{id}")
    void deleteProductById(int id);

    @Select("SELECT * FROM mybatis03.product")
    List<Product> findAll();

    @Results({
            @Result(column = "pid" , property = "id",id = true),
            @Result(column = "pname",property = "name")
    })
    @Select("SELECT id pid, name pname, price, detail, pic, createtime FROM mybatis03.product WHERE id=#{id}")
    Product findById(int id);
}
